/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of JeeStrap Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2014 All rights reserved. =============================
 */

package com.jeestrap.common.smartcoder.generator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeestrap.common.smartcoder.domain.CodeDesc;
import com.jeestrap.common.smartcoder.generator.javafile.ControllerGenerator;
import com.jeestrap.common.smartcoder.generator.javafile.DaoGenerator;
import com.jeestrap.common.smartcoder.generator.javafile.EntityGenerator;
import com.jeestrap.common.smartcoder.generator.javafile.ServiceGenerator;
import com.jeestrap.common.smartcoder.generator.viewfile.ViewFormGenerator;
import com.jeestrap.common.smartcoder.generator.viewfile.ViewListGenerator;
import com.jeestrap.common.smartcoder.util.AppUtil;
import com.jeestrap.common.smartcoder.util.StrUtils;

public class CodeGeneratorEngine {

	private CodeDesc codeDesc;

	private Map<String, Object> model;

	private String separator = "/";

	public CodeGeneratorEngine(CodeDesc codeDesc) {
		this.codeDesc = codeDesc;
		init();
	}

	@SuppressWarnings("null")
	public Map<String, String> genCode() throws Exception {
		Map<String, String> codeMap = new HashMap<String, String>();

		CodeGenerator codeGenerator = null;
		List<String> classNameList = this.codeDesc.getGenClassNameList();
		for (String clsName : classNameList) {
			String code = "";
			if (clsName.contains("Entity.java")) {
				codeGenerator = new EntityGenerator(model);
			} else if (clsName.contains("Controller.java")) {
				codeGenerator = new ControllerGenerator(model);
			} else if (clsName.contains("Service.java")) {
				codeGenerator = new ServiceGenerator(model);
			} else if (clsName.contains("Dao.java")) {
				codeGenerator = new DaoGenerator(model);
			} else if (clsName.contains("List.jsp")) {
				codeGenerator = new ViewListGenerator(model);
			} else if (clsName.contains("Form.jsp")) {
				codeGenerator = new ViewFormGenerator(model);
			}
			try {
				code = codeGenerator.generate(codeDesc);
				codeMap.put(clsName, code);
			} catch (Exception e) {
				System.out.println("gen code error : " + codeGenerator.getClass());
				throw new Exception("gen code error : " + codeGenerator.getClass());
			}
		}
		return codeMap;
	}

	private void init() {
		// 共通变量
		model = new HashMap();
		model.put("packageName", StrUtils.lowerCase(codeDesc.getPackageName()));
		model.put("moduleName", StrUtils.lowerCase(codeDesc.getModuleName()));
		model.put("subModuleName",
			StrUtils.isNotBlank(codeDesc.getSubModuleName()) ? "." + StrUtils.lowerCase(codeDesc.getSubModuleName())
				: "");
		model.put("className", StrUtils.uncapitalize(codeDesc.getClassName()));
		model.put("classAuthor", StrUtils.isNotBlank(codeDesc.getClassAuthor()) ? codeDesc.getClassAuthor()
			: "Generate Tools");
		model.put("classVersion", AppUtil.getDate());
		model.put("functionName", codeDesc.getFunctionName());

		model.put("tableName", codeDesc.getTableName());
		model.put(
			"urlPrefix",
			model.get("moduleName")
				+ (StrUtils.isNotBlank(codeDesc.getSubModuleName()) ? separator
					+ StrUtils.lowerCase(codeDesc.getSubModuleName()) : "") + separator + model.get("className"));
		model.put("viewPrefix", model.get("urlPrefix"));
		model.put("permissionPrefix", model.get("moduleName")
			+ (StrUtils.isNotBlank(codeDesc.getSubModuleName()) ? ":" + StrUtils.lowerCase(codeDesc.getSubModuleName())
				: "") + ":" + model.get("className"));
	}
}
